<?php

namespace app\home\controller;
use cmf\controller\UserBaseController;
use think\Db;

//打印
class PrintController extends UserBaseController{
    public function prlist(){
        $user = cmf_get_current_user();
        $this->assign($user);
        return $this->fetch();
    }
   public function printto(){
       
        $dingdan = $_POST['order'];  
        $liuliu = Db::name('order')->where('orderNo','in',$dingdan)
                ->alias('a')->join('__USER__ b','a.operatorid=b.id','left')
                ->join('__ORDER_DETAIL__ c','a.orderNo=c.orderid','left')
                ->join('__GOODS_STATE__ d','c.nature=d.id','left')
                ->field('a.*,b.user_login,a.orderNo,c.g_goodsname,a.payType,a.getMoney,a.totalMoney,a.discount,a.actualMoney,a.actualMoney,a.giveChange,c.total,c.price,c.count,c.nature,a.ctime,a.num,d.state_name')->select();
        $time = date("Y-m-d H:i:s",$liuliu[0]["ctime"]);//时间戳转化时间格式
        //小票
        //$piao = Db::name("order_detail")->where('orderid','in',$dingdan)->field('sum(count) as sum')->select();
        $order = array(
            array(
                "dingdan" =>$dingdan,
                "付款时间" => $time,
                "shuliang" =>$liuliu[0]["count"],
                "收银员" =>$liuliu[0]["user_login"],
                "现金" =>$liuliu[0]["getMoney"],
                "合计" =>$liuliu[0]["totalMoney"],
                "优惠" =>$liuliu[0]["discount"],
                "应收" =>$liuliu[0]["actualMoney"],
                "实收" =>$liuliu[0]["actualMoney"],
                "找零" =>$liuliu[0]["giveChange"],
		"num"=>$liuliu[0]["num"],//流水号
            )
        );
        foreach ($liuliu as $row) {
            $_r = array(
                "pinming" => $row["g_goodsname"],
                "shuliang" => $row["count"],
                "jiage" => (int)$row["price"],
                "金额" => (int)$row["total"],
                "nature" =>isset($row["state_name"])?$row["state_name"]:'无',
            );
            $order[] = $_r;
        }
        //转成json
        //json_encode($data);
        $jsonStr = json_encode($order);
        //dump($jsonStr);exit;//到这下面的就不执行了
        return $this->success($jsonStr);

    }
    public function printname(){
        $date = Db::name("print")->field("print_name,print_namebq,print_namejj")->find();
        $json = json_encode($date);
        return $this->success($json);
    }
		//打印交接
   public function jiaojie(){
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $start= session("start");//上班开始时间
        $end= session("end");//下班结束时间
        $where['operatorid']=$uinfo['id'];
        $where['storeid']=$uinfo['store_id'];
        $where['payStatus']=1;
        $where['ctime'][]=array('lt',$end);//小于当前时间
        $where['ctime'][]=array('gt',$start);//大于登录
        $payType=array('0'=>'挂单','1'=>'会员卡支付','2'=>'现金支付','3'=>'积分支付','4'=>'微信支付','5'=>'支付宝','6'=>'外卖支付');
        unset($payType['0']);
        
        foreach($payType as $k=>$v){
            $where['payType']=$k;
            $total_money=Db::name('Order')->where($where)->sum('actualMoney');//该店铺该商品总营业额
            $list[$k]['total']=$total_money;
        }
        $map['ctime']=array('lt',$start);//小于当前时间
        $map['ctime']=array('gt',$end);//大于登录
        $map['storeid']=$uinfo['store_id'];//大于登录
        $map['operatorid']=$uid;
        $supply=DB::name('supply')->where($map)->select();
        //查询店铺名称
        $shopname = Db::name("store")->where("id=".$uinfo['store_id'])->find();
        //查询总金额
        $wmoneyz = array();
        $wmoneyz['utime'] = array(array('egt',$start),array('elt',$end));
        $moneyzhong = Db::name("order")->where("paystatus=1")->where($wmoneyz)->field('sum(actualMoney) as sum')->find();
        $moneytui = Db::name("order")->where("paystatus=-3")->where($wmoneyz)->field('sum(actualMoney) as tui')->find();
        $keliu = Db::name("order")->where("paystatus=1")->where($wmoneyz)->count();
        $renjun = (int)$moneyzhong['sum']/(int)$keliu;   
        //封装数组
        $time = date("Y-m-d H:i:s",$start);//时间戳转化时间格式
        $nowtime = date("Y-m-d H:i:s",$end);
        //$zonghe = array($uinfo['user_login'],$time,$nowtime,$list);
        $zonghe = array(
            "交班人"=>$uinfo['user_login'],
            "上班时间"=>$time,
            "下班时间"=>$nowtime,
            "会员卡支付"=>$list[1]["total"],
            "现金支付"=>$list[2]["total"],
            "积分支付"=>$list[3]["total"],
            "微信支付"=>$list[4]["total"],
            "支付宝"=>$list[5]["total"],
            "外卖支付"=>$list[6]["total"],
            "店铺"=>$shopname["store_name"],//店铺名称
            "手机号"=>$uinfo['mobile'],//店长手机号
            "总金额"=>$moneyzhong['sum'],//总额度
            "退款金额"=>(int)$moneytui['tui'],//退款金额
            "客流量"=>$keliu,//客流量
            "人均消费"=>number_format($renjun, 2, '.','')//人均消费
        );
        $json = json_encode($zonghe);
        return $this->success($json);
    }
    //打印交接遍历数据（没有与上面的数组合并）
    public function jiaojieone(){
        $uid= cmf_get_current_user_id();
        //$uinfo=get_operator_data($uid);
        $start= session("start");//上班开始时间
        $end= session("end");//下班结束时间
        $wmoneing['b.utime'] = array(array('egt',$start),array('elt',$end));
        $niubidaima = Db::name("order")->where('a.paystatus=1')->where($wmoneing)->where('a.operatorid='.$uid)
                ->alias('a')->join('__ORDER_DETAIL__ b','a.orderNo=b.orderid')
                ->join('__GOODS__ c','c.g_id=b.goodsid')
                ->join('__GOODS_CLASSIFY__ d','d.id=c.g_category')
                ->field('a.*,d.classify_name,sum(b.money) as zhongmoney,count(b.goodsid) as zhongnum')->group('d.classify_name')->select();
        $jsoncomeno = json_encode($niubidaima);
        return $this->success($jsoncomeno);
    }
    
    public function plist(){
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $list = Db::name("print")->where(array('storeid'=>$uinfo['store_id']))->field('print_id,print_name,print_namebq,print_namejj')->find();
        //dump($list);
        if(empty($list)){           
            $this->success('跳转到添加页面', "print/addpost");           
        }
       
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function addpost(){
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $this->assign('storeid',$uinfo['store_id']);
        return $this->fetch();
    }
    public function adddate(){
        $data = $this->request->param();       
        $result = Db::name('print')->insert($data);
        if($result){
            $this->success(lang('保存成功'), "home/print/plist");
        }else{
            $this->error(lang('保存失败'), "home/print/plist");
        }
    }
    public function updatelist(){
        $printid = $this->request->param("nav_id");
        $uid= cmf_get_current_user_id();
        $uinfo=get_operator_data($uid);
        $list = Db::name('print')->where('print_id='.$printid)->field("print_name,print_namebq,print_namejj,print_id,storeid")->find();
        $this->assign('storeid',$uinfo['store_id']);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function updapost(){
        $data = $this->request->param();        
        $result = Db::name('print')->where("print_id=".$data['print_id'])->update($data);
        if($result){
            $this->success(lang('修改成功'), "print/plist");
        }else{
            $this->error(lang('修改失败'), "print/plist");
        }
    }
    public function gengXin(){
        $url = "http://bft.w209.madgg.com/upload/tbt.exe";
        $extend = @file_get_contents($url);
        $date = array();
        if($extend){
            $date['status'] = 1;
            $date['url']    = $url;
            $this->success($date);
        }else{
            $date['status'] = 2;
            $this->error($date);
        }
    }

}

